<% tags = @products.response["aggregations"]["tags"]["t_name"]["buckets"] %>
<% price_ranges = @products.response["aggregations"]["price_ranges"]["buckets"] %>
<% att_ids =  Attribute.get_attr_id %>
<div class="row">
  <div class="col-md-12">
    <div class="panel-body">
      <%= form_tag products_path, method: 'get' do %>
        <div class="row">
          <div class="col-md-9">
            <div class="form-group mx-sm-3 mb-2">
              <%= text_field_tag "search1[search_word]", params[:search1] && params[:search1][:search_word], class: 'form-control' %>
            </div>
          </div>
          <% tags.each do |tag| %>
            <% tag_id = att_ids[tag['key']] %>
            <% tag_value = (params["tags"] && params["tags"]["#{tag_id}"])%>
            <%= hidden_field_tag "tags[#{tag_id}]", tag_value, id: "ctag1[#{tag_id}]" %>
          <% end %>
          <div class="col-md-3">
            <button type="submit" class="btn btn-primary mb-2">搜索</button>
          </div>
        </div>
      <% end %>
    </div>
    <hr/>
    <div class="panel-body">
      <a data-toggle="collapse" href="#collapseExample" aria-expanded="true" aria-controls="collapseExample">
        详细检索
      </a>
      <div class="collapse show" id="collapseExample">
        <div class="card card-body">
          <%= form_tag products_path, method: 'get' do %>
            <% tags.each do |tag| %>
              <% tag_id = att_ids[tag['key']] %>
              <% tag_value = (params["tags"] && params["tags"]["#{tag_id}"])%>
              <%= hidden_field_tag "tags[#{tag_id}]", tag_value, id: "ctag2[#{tag_id}]" %>
            <% end %>
            <div class="row">
              <div class="col-md-3">
                <div class="form-group mx-sm-3 mb-2">
                  <%= label_tag "商品名" %>
                  <%= text_field_tag "search2[product_name]", params[:search2] && params[:search2][:product_name], class: 'form-control' %>
                </div>
              </div>
              <div class="col-md-6">
                <div class="form-group mx-sm-3 mb-2">
                  <%= label_tag "price_label", "价格区间", style: 'display: block;' %>
                  <%= number_field_tag "search2[price_start]", params[:search2] && params[:search2][:price_start],
                                       class: 'form-control', min: '0', step: '0.01', style: 'width: 47%;display: inline;' %>
                  ～
                  <%= number_field_tag "search2[price_end]", params[:search2] && params[:search2][:price_end],
                                       class: 'form-control', min: '0', step: '0.01', style: 'width: 47%;display: inline;' %>
                </div>
              </div>
              <div class="col-md-3">
                <div class="form-group mx-sm-3 mb-2">
                  <%= label_tag '商品code' %>
                  <%= text_field_tag "search2[store_code]", params[:search2] && params[:search2][:store_code], class: 'form-control' %>
                </div>
              </div>
            </div>
            <div class="row">
              <div class="col-md-12" style="text-align: center">
                <button type="button" class="btn mb-2" onclick="clearConditions()" style="border: 1px #2E2F30 solid;">清除检索条件</button>
                <button type="submit" class="btn btn-primary mb-2">搜索</button>
              </div>
            </div>
          <% end %>
        </div>
      </div>
    </div>
    <hr/>
    <div class="panel-body">
      <% tags.each_with_index do |value| %>
        <div class="form-check form-check-inline">
          <% tag_id = att_ids[value['key']] %>
          <% tag_value = (params["tags"] && params["tags"]["#{tag_id}"])%>
          <%= check_box_tag "tag[#{tag_id}]", value['key'], value['key'] == tag_value, class: 'form-check-input', onchange: "changeTag(#{tag_id})" %>
          <label class="form-check-label" for="tag_<%= tag_id %>"><%= value['key'] %>(<%= value['doc_count'] %>)</label>
        </div>
      <% end %>
    </div>
    <hr/>
    <div class="panel-body">
      <% price_ranges.each_with_index do |range, index| %>
        <% r_params = request.params.deep_dup %>
        <% r_params[:price_range] = index + 1 %>
        <%= link_to "#{range["key"]}(#{range["doc_count"]})", products_path(r_params) %>
      <% end %>
    </div>
    <hr/>
    <div class="panel-body">
      <%
        d_params = request.params.deep_dup
        d_params[:data_order] = 0
      %>
      <%= link_to "默认排序", products_path(d_params) %>
      <% d_params[:data_order] = 1 %>
      <%= link_to "价格升序", products_path(d_params) %>
      <% d_params[:data_order] = 2 %>
      <%= link_to "价格降序", products_path(d_params) %>
    </div>
    <div class="panel-body">
      <table class="table table-striped ">
        <% if @products.present? %>
          <thead>
          <tr>
            <th scope="col">商品名</th>
            <th scope="col">商品code</th>
            <th scope="col">描述</th>
            <th scope="col">价格</th>
          </tr>
          </thead>
          <tbody>
          <% @products.each do |pro| %>
            <tr>
              <th scope="row"><%= pro.name %></th>
              <td><%= pro.store_code %></td>
              <td><%= pro.full_description %></td>
              <td><%= (price = pro.price) ? "¥#{price}" : '' %></td>
            </tr>
          <% end %>
          </tbody>
        <% else %>
          <tbody>
            <tr>
              <th style="text-align: center;">没有数据</th>
            </tr>
          </tbody>
        <% end %>
      </table>
      <% if @products.present? %>
        <%= paginate @products %>
      <% end %>
    </div>
  </div>
</div>
<script>
  let clearConditions = () => {
      $("[name ^= 'search2']").val('');
  }

  let changeTag = (tagId) => {
      let originValue = $(`#tag_${tagId}`).val();
      if (!$(`#tag_${tagId}`).prop('checked')){
          originValue = '';
      }
      $("[name='tags["+tagId+"]'").val(originValue);
  }
</script>
<style>
  .panel-body a{
      margin: 10px 10px;
  }
</style>